package com.xtaller.easy.admin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xtaller.common.convert.V;
import com.xtaller.common.kit.TimeKit;
import com.xtaller.easy.admin.base.TServiceImpl;
import com.xtaller.easy.admin.mapper.SysRoleMapper;
import com.xtaller.easy.admin.model.SysRole;
import com.xtaller.easy.admin.model.SysRoleAuth;
import com.xtaller.easy.admin.model.SysRoleModule;
import com.xtaller.easy.admin.service.ISysRoleService;
import com.xtaller.easy.admin.specification.Auth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import com.xtaller.common.convert.W;
import com.xtaller.common.bean.Where;
import org.springframework.transaction.annotation.Transactional;

/**
* Created by Taller on 2018/04/13
*/
@Service
public class SysRoleService extends TServiceImpl<SysRoleMapper,SysRole> implements ISysRoleService {
    @Autowired
    private SysRoleModuleService roleModuleService;
    @Autowired
    private SysRoleAuthService roleAuthService;
    /**************************CURD begin******************************/
   // 创建
    @Transactional
    @Override
    public SysRole createSysRole(SysRole model, String[] modules) {
        if(this.insert(model)){
            if(modules.length > 0){
                for(String module:modules){
                    SysRoleModule rm = new SysRoleModule();
                    rm.setRoleId(model.getId());
                    rm.setCreator(model.getCreator());
                    rm.setModuleId(module);
                    roleModuleService.createModel(rm);
                }
            }
            return this.selectById(model.getId());
        }
        return null;
    }
 
   // 删除
   @Override
   public Boolean deleteSysRole(Object ids,String reviser) {
       return this.delete(ids,reviser);
   }
 
   // 修改
    @Transactional
    @Override
    public SysRole updateSysRole(SysRole model, String[] modules) {
       if(this.update(model)) {
           SysRoleModule w = new SysRoleModule();
           w.setReviser(model.getReviser());
           w.setReviseTime((int) TimeKit.getTimestamp());
           w.setRoleId(model.getId());
           baseMapper.initRoleModule(w);
           if(modules.length > 0){
               for(String module:modules){
                   SysRoleModule rm = new SysRoleModule();
                   rm.setCreator(model.getCreator());
                   rm.setRoleId(model.getId());
                   rm.setModuleId(module);
                   roleModuleService.createModel(rm);
               }
           }
           return this.selectById(model.getId());
       }
       return null;
    }
 
   // 查询
   @Override
   public List<SysRole> findByIds(Object ids) {
       return this.selectByIds(ids);
   }
 
   // 属于
   @Override
   public Boolean exist(List<Where> w) {
       w.add(new Where("1"));
       return this.query(w).size() > 0;
   }
 
   // 查询一个id是否存在
   @Override
   public Boolean existId(Object id) {
       where = W.f(
               W.and("id","eq",id),
               W.field("1")
       );
       return this.query(where).size() > 0;
   }



    /**************************CURD end********************************/
    @Override
    public Boolean existName(SysRole model, boolean isCreate) {
        if(!isCreate){
            where = W.f(
                    W.and("name", "eq", model.getName()),
                    W.and("id", "ne", model.getId())
            );
        }else{
            where = W.f(
                    W.and("name", "eq", model.getName())
            );
        }
        return this.queryOne(where) == null ? false : true;
    }

    @Override
    public List<JSONObject> getRole() {
        where = W.f(
                W.field("id", "name","level"),
                W.order("createTime", "asc")
        );

        List<SysRole> roles = this.query(where);
        List<JSONObject> data = new ArrayList<>();
        if(roles.size() > 0){
            // 加入角色认证模块
            List<JSONObject> roleModule = baseMapper.getRoleModule();
            for(SysRole role:roles){
                JSONObject object = new JSONObject();
                List<JSONObject> modules = new ArrayList<>();
                if(roleModule.size() > 0){
                    for(JSONObject rm:roleModule){
                        if(V.isEqual(rm.getString("roleId"), role.getId())){
                            modules.add(rm);
                        }
                    }
                }
                object.put("id", role.getId());
                object.put("name", role.getName());
                object.put("level", role.getLevel());
                object.put("remark", role.getRemark());
                object.put("module", modules);
                data.add(object);
            }
        }
        return data;
    }

    @Override
    public List<JSONObject> getByRoleId(String roleId) {
        return baseMapper.getByRole(roleId);
    }

    @Transactional
    @Override
    public Boolean setAuth(List<Auth> auths, String roleId, String creator) {
        // 初始化权限信息
        baseMapper.initAuth(roleId);
        // 处理数据
        if(auths.size() > 0){
            for(Auth auth:auths){
                // 处理菜单
                SysRoleAuth roleAuth = new SysRoleAuth();
                roleAuth.setRoleId(roleId);
                roleAuth.setMenuId(auth.getMenuId());
                roleAuth.setCreator(creator);
                roleAuthService.insert(roleAuth);
                // 处理按钮
                if(auth.getBtns().length > 0){
                    for(String b:auth.getBtns()) {
                        SysRoleAuth btn = new SysRoleAuth();
                        btn.setRoleId(roleId);
                        btn.setMenuId(auth.getMenuId());
                        btn.setAuthId(b);
                        btn.setIsMenu(0);
                        btn.setCreator(creator);
                        roleAuthService.insert(btn);
                    }
                }
            }
        }
        return true;
    }
}